#!/opt/local/bin/python

import sys, os, random, math

import pygame
try:
    import pygame._view
except:
    pass
from pygame.locals import *
from pygame.color import *

import constants as con
import deathandtaxes

def main():
    clock = pygame.time.Clock()
    realtime = 0
    curbeat = 0

    timer = 0
    
    game = deathandtaxes.Game()

    #############################
    ##Start of main loop        #
    now = pygame.time.get_ticks()
    while not game.exit:

        for event in pygame.event.get():
            if event.type == QUIT:
                game.exit = True
            elif event.type == KEYDOWN:
                keyDown(event.key,game)
            elif event.type == KEYUP:
                keyUp(event.key,game)
            elif event.type == MOUSEBUTTONDOWN:
                mouseDown(event.button,game)

        if game.state == 'Play':
            if timer >= 1000:
                game.timer -= 1
                timer = 0
            if realtime >= game.beat:
                game.thump = 1
                #print "thump (%d/%d) %d: %s" % (realtime,game.beat,curbeat,game.pattern[curbeat])
                if game.pattern[curbeat] == 'b':
                    game.newBrick()
                elif game.pattern[curbeat] == 'c':
                    game.newCoin()
                    game.newCoin()
                    game.newCoin()

                realtime = 0
                curbeat += 1
                if curbeat > 3:
                    curbeat = 0
            else:
                game.thump = 0

            # doing multiple steps seems to make the simulation
            # feel "heavier"
            for x in range(10):
                game.space.step(con.dt)

        elif game.state == 'Menu':
            if realtime >= 50:
                if not game.bpmadjusting == 0:
                    game.bpm += game.bpmadjusting
                    
                realtime = 0

        if game.state == 'Play':
            game.update()
            game.player.chaseMouse(game)

        game.fps = clock.get_fps()
        game.redraw()
        buf = clock.tick_busy_loop(60)
        realtime += buf
        timer += buf

    pygame.quit(); sys.exit()

def keyDown(key,game):
    if key == K_ESCAPE:
        game.exit = True

    if game.state == 'Menu':
        game.mainmenu.inputDown(key)
    elif game.state == 'Scores':
        game.topscores.inputDown(key)
    elif game.state == 'Instructions':
        game.instructions.inputDown(key)
    elif game.state == 'Postgame':
        game.postgame.inputDown(key)
    elif game.state == 'Play':
        if key == K_n:
            #print "making brick"
            game.newBrick()
        elif key == K_m:
            #print "making coin"
            game.newCoin()
            game.newCoin()
        elif key == K_b:
            #print "making both"
            game.newBrick()
            game.newCoin()
            game.newCoin()
        elif key == K_v:
            print game.sprites.sprites()

def keyUp(key,game):
    if game.state == 'Menu':
        game.mainmenu.inputUp(key)

def mouseDown(key,game):
    if game.state == 'Play':
        if key == 1:
            game.mouseDown()

if __name__ == "__main__":
    main()

